package com.university.trade.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.university.trade.util.StringUtil;

public class BaseAction {
	@SuppressWarnings("unused")
	private final static Logger logger = Logger.getLogger(BaseAction.class);
	public final static String SUCCESS = "success";
	public final static String ERROR = "error";
	public final static String FAIL = "fail";
	public final static String OK = "ok";
	public final static String MSG = "msg";
	public final static String MOBILE_MSG = "mobile_msg";
	public static Map<String,Object> sessionMap = new HashMap<String,Object>();

	protected void printJson(String json, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(json);
		out.flush();
		out.close();
	}

	protected void printText(String text, HttpServletResponse response) throws IOException {
		response.setContentType("text/plain;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(text);
		out.flush();
		out.close();
	}

	protected void generateDownloadFile(String inFile, String downloadFile, HttpServletResponse response)
			throws IOException {
		InputStream ins = new BufferedInputStream(new FileInputStream(inFile));
		byte[] buffer = new byte[ins.available()];
		ins.read(buffer);
		ins.close();
		response.reset();
		response.addHeader("Content-Disposition", "attachment;filename=" + new String(downloadFile.getBytes()));
		response.addHeader("Content-Length", "" + new File(inFile).length());
		OutputStream ous = new BufferedOutputStream(response.getOutputStream());
		response.setContentType("application/octet-stream");
		ous.write(buffer);
		ous.flush();
		ous.close();
	}

	protected void export(String infile, String downloadFile, HttpServletResponse response) throws Exception {
		File inFile = new File(infile);
		InputStream ins = new BufferedInputStream(new FileInputStream(infile));
		byte[] buffer = new byte[ins.available()];
		ins.read(buffer);
		ins.close();
		response.reset();
		response.addHeader("Content-Disposition", "attachment;filename=" + new String(downloadFile.getBytes()));
		response.addHeader("Content-Length", "" + inFile.length());
		OutputStream ous = new BufferedOutputStream(response.getOutputStream());
		response.setContentType("application/octet-stream");
		ous.write(buffer);
		ous.flush();
		ous.close();
	}

	/**
	 * 检查移动端表单提交的
	 * 
	 * @param name
	 * @return
	 */
	protected String checkMoblieToken(String name, String key, HttpServletRequest request) {
		String paramValue = request.getParameter(name);
		String tokenValue = StringUtil.isNull((String) sessionMap.get(key));
		// 参数、session中都没用token值提示错误
		if (paramValue.isEmpty() && tokenValue.isEmpty()) {
			// throw new BorrowException("会话Token未设定！");
			return "会话Token未设定！";
		} else if (paramValue.isEmpty() && !tokenValue.isEmpty()) {
			// throw new BorrowException("表单Token未设定！");
			return "表单Token未设定！";
		} else if (paramValue.equals(tokenValue) && !tokenValue.isEmpty()) { // session中有token,防止重复提交检查
			sessionMap.remove(key);
			return "";
		} else {
			// throw new BorrowException("请勿重复提交！");
			return "请勿重复提交！";
		}
	}

	protected String saveMoblieToken(String name, String userId, HttpServletRequest request) {
		String token = UUID.randomUUID().toString();
		sessionMap.put(name + "_" + userId, token);
		return token;
	}
}
